package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.DateUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.collect.ListUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.manhua.dal.IPageDataDAL;
import com.qire.manhua.model.viewModel.impl.RecommendUpdateViewModel;
import com.qire.manhua.model.vo.HomeRecommendUpdateItemVO;

import java.util.List;

public class RecommendUpdateService extends DataService {

    private IPageDataDAL pageData = WarehouseFactory.create(this, IPageDataDAL.class);

    private final RecommendUpdateViewModel viewModel;

    public RecommendUpdateService(RecommendUpdateViewModel viewModel) {
        this.viewModel = viewModel;
    }

    // 更新数据
    public void loadPageData() {
        pageData.fetchRecommendUpdatePageData(viewModel.pageNo(), viewModel.timeStamp());
    }

    @StrategyAnnotation(event = Url.HomeRecommendUpdateData)
    public void fetchRecommendUpdatePageDataCallBack(List<HomeRecommendUpdateItemVO> dataVOList, boolean isCache) {

        viewModel.refreshLayoutFinish(300);
        // 没有更多数据了
        if (ListUtil.isEmpty(dataVOList)) {
            viewModel.noMoreDataWasShown();
            return;
        }

        //缓存并不会增加页面，所以第一次进入缓存会先使用不增加页面，非缓存进入时页面怎加，清空缓存记录重新插入。
        if (viewModel.isFirstPage()) {
            viewModel.clearUpdateComicList();
        }

        for(HomeRecommendUpdateItemVO dataVO : dataVOList){
            //不确定是否需要过滤重复数据，如果发现有重复数据在此处添加判断过滤

            int    comicId = dataVO.id;
            String comicName = dataVO.book_name;
            String coverImg = dataVO.book_cover;
            int    latestChapterId = dataVO.last_chapter_id;
            String chapterTitle = dataVO.last_chapter_name;
            String typeTag = dataVO.cate_name;
            String typeTagBgColor = "#" + dataVO.cate_color;
            String updateTime = StringUtil.friendlyDay(DateUtil.yMdHms.parse(dataVO.last_chapter_time)) + "更新";

            viewModel.addUpdateComicItem(comicId, comicName, coverImg, latestChapterId, chapterTitle, typeTag, typeTagBgColor, updateTime);
        }

        viewModel.refreshUpdateComicAdapter();
        if (!isCache) {
            viewModel.nextPage();
        }

        viewModel.refreshErrorDataUI();

    }




}
